package com.app.service.impl;

import com.app.common.Result;
import com.app.mapper.SupervisionMapper;
import com.app.model.*;
import com.app.service.SearchService;
import com.app.service.SupervisionService;
import com.app.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.StreamUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SupervisionServiceImpl implements SupervisionService {
    @Autowired
    private SupervisionMapper supervisionMapper;

    @Override
    public SupervisionVo selectAll(String name, Integer type, String startTime, String endTime,Integer dutiesId) {
        count(type);
        //查职务
        List<DutiesModel> dutiesModelList = supervisionMapper.selectDutiesByDutiesId(dutiesId);
        List<DutiesModel> duties = supervisionMapper.Duties();
        //查事项
        List<MatterModel> projectModelList = supervisionMapper.selectProject(name, startTime, endTime);
        if (projectModelList.isEmpty()) {
            for (int i = 0; i < dutiesModelList.size(); i++) {
                dutiesModelList.get(i).setProject(Collections.emptyList());
            }
        }
        for (int i = 0; i < projectModelList.size(); i++) {
            for (int i1 = 0; i1 < duties.size(); i1++) {
                if (Objects.equals(projectModelList.get(i).getDutyName(), duties.get(i1).getId())) {
                    projectModelList.get(i).setDutyNameText(duties.get(i1).getName());
                }
            }
        }
        for (int i = 0; i < projectModelList.size(); i++) {
            Integer id = projectModelList.get(i).getId();
            List<ProgressModel> progressModelList = supervisionMapper.selectProgress(id);
            if (progressModelList != null) {
                projectModelList.get(i).setProgress(progressModelList);
            }
        }
        for (int i = 0; i < dutiesModelList.size(); i++) {
            List<MatterModel> list = new ArrayList<>();
            for (int i1 = 0, j = 0; i1 < projectModelList.size(); i1++) {
                if (Objects.equals(dutiesModelList.get(i).getId(), projectModelList.get(i1).getDutiesId())) {
                    list.add(j, projectModelList.get(i1));
                    j++;
                    dutiesModelList.get(i).setProject(list);
                } else {
                    dutiesModelList.get(i).setProject(list);
                }
            }
        }
        SupervisionVo vo = new SupervisionVo();
        vo.setCount(dutiesModelList.size());
        vo.setList(dutiesModelList);
        return vo;
    }

    @Override
    public SupervisionVo getAll(Integer id, String name, String startTime, Integer type) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        //查职务
        List<DutiesModel> dutiesModelList = supervisionMapper.getDuties(id, name);
        if (type != null && type == 2) {
            List<ReportModel> models = new ArrayList<>();
            SupervisionVo vo = new SupervisionVo();
            for (int i = 0; i < dutiesModelList.size(); i++) {
                Integer id1 = dutiesModelList.get(i).getId();
                if (startTime == null || startTime.equals("")) {
                    startTime = simpleDateFormat.format(date);
                }
                List<ReportModel> reportModels = supervisionMapper.selectReport(id1, startTime);
                if (reportModels != null) {
                    for (int i1 = 0; i1 < reportModels.size(); i1++) {
                        if (reportModels.get(i1).getTime() == 1) {
                            reportModels.get(i1).setMorningText(reportModels.get(i1).getReport());
                            reportModels.get(i1).setMorningLocation(reportModels.get(i1).getLocation());
                            for (int j = 0; j < reportModels.size(); j++) {
                                if (reportModels.get(j).getTime() == 2 && reportModels.get(j).getDutiesId().equals(reportModels.get(i1).getDutiesId())) {
                                    reportModels.get(i1).setAfternoonText(reportModels.get(j).getReport());
                                    reportModels.get(i1).setAfternoonLocation(reportModels.get(j).getLocation());
                                }
                            }
                        } else {
                            reportModels.get(i1).setAfternoonText(reportModels.get(i1).getReport());
                            reportModels.get(i1).setAfternoonLocation(reportModels.get(i1).getLocation());
                            for (int j = 0; j < reportModels.size(); j++) {
                                if (reportModels.get(j).getTime() == 1 && reportModels.get(j).getDutiesId().equals(reportModels.get(i1).getDutiesId())) {
                                    reportModels.get(i1).setMorningText(reportModels.get(j).getReport());
                                    reportModels.get(i1).setMorningLocation(reportModels.get(j).getLocation());
                                }
                            }
                        }
                        for (int i2 = 0; i2 < dutiesModelList.size(); i2++) {
                            if (reportModels.get(i1).getDutiesId().equals(dutiesModelList.get(i2).getId())) {
                                reportModels.get(i1).setName(dutiesModelList.get(i2).getName());
                            }
                        }
                    }
                }
                assert reportModels != null;
                models.addAll(reportModels);
            }
            List<ReportModel> collect = models.stream()
//                    .filter(ReportModel -> ReportModel.getMorningText() != null || ReportModel.getAfternoonText() != null)
                    .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ReportModel::getDutiesId))), ArrayList::new));
            vo.setReportModel(collect);
            return vo;
        }
        for (int i = 0; i < dutiesModelList.size(); i++) {
            Integer id1 = dutiesModelList.get(i).getId();
            startTime = null;
            List<ReportModel> reportModels = supervisionMapper.selectReport(id1, startTime);
            if (reportModels != null) {
                dutiesModelList.get(i).setReport(reportModels);
            } else {
                dutiesModelList.get(i).setReport(Collections.emptyList());
            }
        }
        SupervisionVo vo = new SupervisionVo();
        vo.setCount(dutiesModelList.size());
        vo.setList(dutiesModelList);
        return vo;
    }

    @Override
    public List<SystemModel> selectType() {
        return supervisionMapper.selectType();
    }

    @Override
    public ReportVo getList(Integer page, Integer limit, Integer pid, String report) {
        page = (page - 1) * limit;
        //查职务
        List<DutiesModel> dutiesModelList = supervisionMapper.Duties();
        List<ReportModel> reportModels = supervisionMapper.selectReportList(page, limit, report);
        List<ReportModel> reportModelList = new ArrayList<>();
        for (int i = 0; i < reportModels.size(); i++) {
            for (int i1 = 0; i1 < dutiesModelList.size(); i1++) {
                if (Objects.equals(reportModels.get(i).getDutiesId(), dutiesModelList.get(i1).getId())) {
                    reportModels.get(i).setDuties(dutiesModelList.get(i1).getDuties());
                    reportModels.get(i).setName(dutiesModelList.get(i1).getName());
                    reportModels.get(i).setPid(dutiesModelList.get(i1).getPid());
                }
            }
        }
        if (pid != 0) {
            for (int i = 0, j = 0; i < reportModels.size(); i++) {
                if (Objects.equals(reportModels.get(i).getPid(), pid)) {
                    reportModelList.add(j, reportModels.get(i));
                }
            }
            ReportVo vo = new ReportVo();
            vo.setCount(reportModelList.size());
            vo.setList(reportModelList);
            return vo;
        }
        Integer count = supervisionMapper.getCount();
        ReportVo vo = new ReportVo();
        vo.setCount(count);
        vo.setList(reportModels);
        return vo;
    }

    @Override
    public MatterVo selectList(Integer page, Integer limit, Integer type, String matterName) {
        page = (page - 1) * limit;
        //查职务
        List<DutiesModel> dutiesModelList = supervisionMapper.selectDuties();
        List<DutiesModel> duties = supervisionMapper.Duties();
        //查事项
        List<MatterModel> projectModelList = supervisionMapper.selectMatter(page, limit, matterName);
        for (int i = 0; i < projectModelList.size(); i++) {
            Integer id = projectModelList.get(i).getId();
            List<ProgressModel> progressModelList = supervisionMapper.selectProgress(id);
            if (progressModelList != null) {
                projectModelList.get(i).setProgress(progressModelList);
            } else {
                projectModelList.get(i).setProgress(Collections.emptyList());
            }
        }
        for (int i = 0; i < projectModelList.size(); i++) {
            for (int i1 = 0; i1 < dutiesModelList.size(); i1++) {
                if (Objects.equals(dutiesModelList.get(i1).getId(), projectModelList.get(i).getDutiesId())) {
                    projectModelList.get(i).setDuties(dutiesModelList.get(i1).getDuties());
                    projectModelList.get(i).setName(dutiesModelList.get(i1).getName());
                }
            }
        }
        for (int i = 0; i < projectModelList.size(); i++) {
            for (int i1 = 0; i1 < duties.size(); i1++) {
                if (Objects.equals(projectModelList.get(i).getDutyName(), duties.get(i1).getId())) {
                    projectModelList.get(i).setDutyNameText(duties.get(i1).getName());
                }
            }
        }
        Integer count = supervisionMapper.getProjectCount();
        MatterVo vo = new MatterVo();
        vo.setCount(count);
        vo.setList(projectModelList);
        return vo;
    }

    @Override
    public ProgressVo selectProgress(Integer matterId) {
        List<ProgressModel> progressModelList = supervisionMapper.selectProgress(matterId);
        ProgressVo vo = new ProgressVo();
        vo.setList(progressModelList);
        vo.setCount(progressModelList.size());
        return vo;
    }

    @Override
    public SystemVo selectDutiesByName(String name) {
        if (name != null && !name.equals("")) {
            List<DutiesModel> dutiesModelList = supervisionMapper.selectDutiesByName(name);
            for (int i = 0; i < dutiesModelList.size(); i++) {
                Integer id1 = dutiesModelList.get(i).getId();
                List<ReportModel> reportModels = supervisionMapper.selectReport(id1);
                if (reportModels != null) {
                    dutiesModelList.get(i).setReport(reportModels);
                } else {
                    dutiesModelList.get(i).setReport(Collections.emptyList());
                }
            }
            List<SystemModel> systemModels = selectType();
            for (int i = 0; i < systemModels.size(); i++) {
                for (int i1 = 0; i1 < dutiesModelList.size(); i1++) {
                    if (systemModels.get(i).getId() == dutiesModelList.get(i1).getPid()) {
                        systemModels.get(i).setList(dutiesModelList.get(i1));
                    }
                }
            }
            SystemVo vo = new SystemVo();
            vo.setCount(systemModels.size());
            vo.setList(systemModels);
            return vo;
        }
        SystemVo vo = new SystemVo();
        vo.setCount(selectType().size());
        vo.setList(selectType());
        return vo;
    }

    @Override
    public MatterVo superintendentSecretary(Integer pid, String startTime, Integer type) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        //查职务
        List<DutiesModel> dutiesModelList = supervisionMapper.selectDuties();
        List<DutiesModel> duties = supervisionMapper.Duties();
        //查事项
        if (type != null && type == 2) {
            if (startTime == null || startTime.equals("")) {
                startTime = simpleDateFormat.format(date);
            }
            List<MatterModel> projectModelList = supervisionMapper.superintendentSecretary(pid, startTime);
            for (int i = 0; i < projectModelList.size(); i++) {
                Integer id = projectModelList.get(i).getId();
                List<ProgressModel> progressModelList = supervisionMapper.selectProgress(id);
                if (progressModelList != null) {
                    projectModelList.get(i).setProgress(progressModelList);
                } else {
                    projectModelList.get(i).setProgress(Collections.emptyList());
                }
            }
            for (int i = 0; i < projectModelList.size(); i++) {
                for (int i1 = 0; i1 < dutiesModelList.size(); i1++) {
                    if (Objects.equals(projectModelList.get(i).getDutiesId(), dutiesModelList.get(i1).getId())) {
                        projectModelList.get(i).setDutiesModel(dutiesModelList.get(i1));
                    }
                }
            }
            for (int i = 0; i < projectModelList.size(); i++) {
                for (int i1 = 0; i1 < duties.size(); i1++) {
                    if (Objects.equals(projectModelList.get(i).getDutyName(), duties.get(i1).getId())) {
                        projectModelList.get(i).setDutyNameText(duties.get(i1).getName());
                    }
                }
            }
            MatterVo vo = new MatterVo();
            vo.setList(projectModelList);
            vo.setCount(projectModelList.size());
            return vo;
        }
        List<MatterModel> projectModelList = supervisionMapper.superintendentSecretary(pid, startTime);
        for (int i = 0; i < projectModelList.size(); i++) {
            Integer id = projectModelList.get(i).getId();
            List<ProgressModel> progressModelList = supervisionMapper.selectProgress(id);
            if (progressModelList != null) {
                projectModelList.get(i).setProgress(progressModelList);
            } else {
                projectModelList.get(i).setProgress(Collections.emptyList());
            }
        }
        for (int i = 0; i < projectModelList.size(); i++) {
            for (int i1 = 0; i1 < dutiesModelList.size(); i1++) {
                if (Objects.equals(projectModelList.get(i).getDutiesId(), dutiesModelList.get(i1).getId())) {
                    projectModelList.get(i).setDutiesModel(dutiesModelList.get(i1));
                }
            }
        }
        for (int i = 0; i < projectModelList.size(); i++) {
            for (int i1 = 0; i1 < duties.size(); i1++) {
                if (Objects.equals(projectModelList.get(i).getDutyName(), duties.get(i1).getId())) {
                    projectModelList.get(i).setDutyNameText(duties.get(i1).getName());
                }
            }
        }
        MatterVo vo = new MatterVo();
        vo.setList(projectModelList);
        vo.setCount(projectModelList.size());
        return vo;
    }

    private int count = 0;

    @Override
    public void redCount() {
        count++;
    }

    @Override
    public Integer getCount() {
        return count;
    }

    @Override
    public SupervisionVo getByPid(Integer pid) {
        List<DutiesModel> duties = supervisionMapper.getDuties(pid, null);
        SupervisionVo vo = new SupervisionVo();
        vo.setCount(duties.size());
        vo.setList(duties);
        return vo;
    }

    @Override
    public MatterVo selectByTime(String startTime, String endTime) {
        supervisionMapper.selectByTime(startTime, endTime);
        return null;
    }

    private void count(Integer id) {
        if (id != null && id == 1) {
            count = 0;
        }
    }
}
